package com.tsinghuabigdata.edu.ddmath.module.neteaseim.education.activity;

import android.content.Context;
import android.content.Intent;
import android.os.Bundle;
import android.provider.Settings;
import android.support.v4.app.FragmentManager;
import android.support.v4.app.FragmentTransaction;
import android.text.TextUtils;
import android.util.Log;
import android.view.View;
import android.view.ViewGroup;
import android.view.WindowManager;
import android.widget.FrameLayout;
import android.widget.Toast;

import com.facebook.stetho.common.LogUtil;
import com.netease.nimlib.sdk.AbortableFuture;
import com.netease.nimlib.sdk.NIMClient;
import com.netease.nimlib.sdk.Observer;
import com.netease.nimlib.sdk.RequestCallback;
import com.netease.nimlib.sdk.ResponseCode;
import com.netease.nimlib.sdk.StatusCode;
import com.netease.nimlib.sdk.auth.AuthServiceObserver;
import com.netease.nimlib.sdk.chatroom.ChatRoomService;
import com.netease.nimlib.sdk.chatroom.ChatRoomServiceObserver;
import com.netease.nimlib.sdk.chatroom.model.ChatRoomInfo;
import com.netease.nimlib.sdk.chatroom.model.ChatRoomKickOutEvent;
import com.netease.nimlib.sdk.chatroom.model.ChatRoomMember;
import com.netease.nimlib.sdk.chatroom.model.ChatRoomStatusChangeData;
import com.netease.nimlib.sdk.chatroom.model.EnterChatRoomData;
import com.netease.nimlib.sdk.chatroom.model.EnterChatRoomResultData;
import com.netease.nimlib.sdk.msg.model.IMMessage;
import com.netease.nimlib.sdk.rts.RTSCallback;
import com.netease.nimlib.sdk.rts.RTSChannelStateObserver;
import com.netease.nimlib.sdk.rts.RTSManager2;
import com.netease.nimlib.sdk.rts.constant.RTSTunnelType;
import com.netease.nimlib.sdk.rts.model.RTSData;
import com.netease.nimlib.sdk.rts.model.RTSTunData;
import com.tsinghuabigdata.edu.ddmath.MVPModel.RequestListener;
import com.tsinghuabigdata.edu.ddmath.R;
import com.tsinghuabigdata.edu.ddmath.commons.http.HttpResponse;
import com.tsinghuabigdata.edu.ddmath.commons.newbieguide.ScreenUtils;
import com.tsinghuabigdata.edu.ddmath.dialog.PermissionDialog;
import com.tsinghuabigdata.edu.ddmath.module.mycenter.SetHeadImage;
import com.tsinghuabigdata.edu.ddmath.module.neteaseim.ModuleProxy;
import com.tsinghuabigdata.edu.ddmath.module.neteaseim.NimCache;
import com.tsinghuabigdata.edu.ddmath.module.neteaseim.base.ui.TActivity;
import com.tsinghuabigdata.edu.ddmath.module.neteaseim.education.doodle.Transaction;
import com.tsinghuabigdata.edu.ddmath.module.neteaseim.education.doodle.TransactionCenter;
import com.tsinghuabigdata.edu.ddmath.module.neteaseim.education.helper.ChatRoomMemberCache;
import com.tsinghuabigdata.edu.ddmath.module.neteaseim.education.helper.VideoListener;
import com.tsinghuabigdata.edu.ddmath.module.neteaseim.education.util.Preferences;
import com.tsinghuabigdata.edu.ddmath.module.neteaseim.fragment.ChatRoomFragment;
import com.tsinghuabigdata.edu.ddmath.module.neteaseim.fragment.ChatRoomRTSFragment;
import com.tsinghuabigdata.edu.ddmath.module.neteaseim.fragment.TestRTSFragment;
import com.tsinghuabigdata.edu.ddmath.module.onlineTutoring.TutorModel;
import com.tsinghuabigdata.edu.ddmath.module.onlineTutoring.bean.ApplyTutor;
import com.tsinghuabigdata.edu.ddmath.module.onlineTutoring.bean.TutorState;
import com.tsinghuabigdata.edu.ddmath.util.AlertManager;
import com.tsinghuabigdata.edu.ddmath.util.DataUtils;
import com.tsinghuabigdata.edu.ddmath.util.DateUtils;
import com.tsinghuabigdata.edu.ddmath.util.DensityUtils;
import com.tsinghuabigdata.edu.ddmath.util.GlobalData;
import com.tsinghuabigdata.edu.ddmath.util.PermissionUtil;
import com.tsinghuabigdata.edu.ddmath.util.ToastUtils;
import com.tsinghuabigdata.edu.ddmath.view.MyProgressDialog;

import java.io.UnsupportedEncodingException;
import java.util.ArrayList;
import java.util.List;
import java.util.Map;

import static com.tsinghuabigdata.edu.ddmath.module.neteaseim.NimCache.getContext;

/**
 * Created by hzxuwen on 2016/2/29.
 */
public class TestRoomActivity extends TActivity {
    private static final String TAG = TestRoomActivity.class.getSimpleName();

    private TutorModel mTutorModel = new TutorModel();

    private ChatRoomFragment fragment;
    private TestRTSFragment  rtsFragment;

    private AbortableFuture<EnterChatRoomResultData> enterRequest;

    private Context          context;
    private String           mAccessToken;
    private String           mStudentId;
    private String           mTeacherId;
    private String           mClassId;
    private int              mCoachId;
    private MyProgressDialog progressDialog;

    @Override
    protected int getContentViewId() {
        return R.layout.test_chat_room_activity;
    }


    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        context = this;
        getWindow().addFlags(WindowManager.LayoutParams.FLAG_KEEP_SCREEN_ON);

        initView();
    }

    private void initView() {
        setTitle("Test");
        setBarTextcolor(R.color.white);
        setUserBarBackgroud(true);

        FrameLayout flLive = (FrameLayout) findViewById(R.id.fl_live);
        ViewGroup.LayoutParams layoutParams = flLive.getLayoutParams();
        int videoHeight = ScreenUtils.getScreenContentHeight(this) - DensityUtils.dp2px(this, 20 + 84);
        layoutParams.width = videoHeight / 2 + DensityUtils.dp2px(this, 40);
        flLive.setLayoutParams(layoutParams);

        rtsFragment = new TestRTSFragment();
        fragment = new ChatRoomFragment();
        FragmentManager fragmentManager = getSupportFragmentManager();
        FragmentTransaction fragmentTransaction = fragmentManager.beginTransaction();
        fragmentTransaction.add(R.id.fl_doodle, rtsFragment).commit();

        progressDialog = new MyProgressDialog(context);
        progressDialog.setMessage("加载中...");
        progressDialog.setCanceledOnTouchOutside(false);
        findViewById(R.id.tv_start).setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                startTutor();
            }
        });
        findViewById(R.id.tv_end).setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                endTutor();
            }
        });
    }

    //进入辅导
    private void startTutor() {
        Log.i(TAG, "startTutor() mCoachId=" + mCoachId);
        String remark = "remark——startTutor";
        mAccessToken = "eyJyIjo4MTMsIm8iOjkxOTQsInN5cyI6IjEwMDAwIiwiYSI6IjMyOEE0QzczMTU5NzQzRDlCOUU1#%#OTQwNzJCRjU5NTBDIiwidCI6ImEifQ#%#";
        mCoachId = 9;
        mTutorModel.startTutor(mAccessToken, mCoachId, remark, new RequestListener<TutorState>() {

            @Override
            public void onSuccess(TutorState vo) {
                Log.i(TAG, "startTutor onSuccess");
            }

            @Override
            public void onFail(HttpResponse<TutorState> response, Exception ex) {
                Log.i(TAG, "startTutor onFail");
            }
        });
    }

    //结束辅导
    private void endTutor() {
        Log.i(TAG, "startTutor() mCoachId=" + mCoachId);
        String remark = "remark——endTutor";
        mAccessToken = "eyJyIjo4MTMsIm8iOjkxOTQsInN5cyI6IjEwMDAwIiwiYSI6IjMyOEE0QzczMTU5NzQzRDlCOUU1#%#OTQwNzJCRjU5NTBDIiwidCI6ImEifQ#%#";
        mCoachId = 9;
        mTutorModel.endTutorNormal(mAccessToken, mCoachId, remark, new RequestListener<TutorState>() {

            @Override
            public void onSuccess(TutorState vo) {
                Log.i(TAG, "startTutor onSuccess");
                if (vo == null) {
                    return;
                }
            }

            @Override
            public void onFail(HttpResponse<TutorState> response, Exception ex) {
                Log.i(TAG, "endTutorNormal onFail");
                ToastUtils.showShort(context, R.string.server_error);
            }
        });
    }
}
